#include "RTC.h"
#include "stdio.h"
#include "lcd_driver.h"
#include "time.h"
#include "key.h"


struct tm init_time={0,30,10,1,10-1,2025-1900};
char buff[64]="\0";


void RTC_Configuration(void)
{
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);


  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}


void NVIC_RTC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void RTC_config()
{
	RTC_Configuration();
	NVIC_RTC_Configuration();
	
}


/**
  * @brief  Adjusts time.
  * @param  None
  * @retval None
  */
void Time_Adjust(void)
{
	u32 counter=0;
	counter=mktime(&init_time);
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Change the current time */
  RTC_SetCounter(counter);//???0????
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}



void RTC_IRQHandler(void)
{
	double un1=0;
	double un2=0;
	double un3=0;
	u32 now_count =0;
//	char buff[64]="\0";
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    /* Clear the RTC Second interrupt */
		now_count=RTC_GetCounter();
		struct tm *ptime =localtime (&now_count);
		
		sprintf(buff,"%4d-%2d-%2d %2d:%2d:%2d %1d",
		              ptime->tm_year+1900,
                  ptime->tm_mon+1,
		              ptime->tm_mday,
		              ptime->tm_hour,ptime->tm_min,ptime->tm_sec,
		              ptime->tm_wday
	    
		);
		if(Key_value=='5')
		{
			LCD_ShowString(0,0,(u8 *)buff,BLACK,WHITE,16,0);
			un1=(ptime->tm_sec)*6;
		un2=(ptime->tm_min)*6;
		un3=(ptime->tm_hour)*30+(ptime->tm_min)*0.5;
		if(ptime->tm_min%1==0&&ptime->tm_sec==0)
		{			
    LcdDrawAngleLine2(120,180,(un3-0.5)-90,50,WHITE);
		}
		LcdDrawAngleLine2(120,180,un1-90,50,BLACK);	
		LcdDrawAngleLine2(120,180,(un1-6)-90,50,WHITE);
		LcdDrawAngleLine2(120,180,un2-90,40,BLACK);
		LcdDrawAngleLine2(120,180,(un2-6)-90,50,WHITE);
		
		LcdDrawAngleLine2(120,180,un3-90,30,BLACK);
		
		Draw_Circlemy(120,180,2,BLACK);
		Draw_Circlemy(120,180,1,BLACK);
	  LCD_DrawPoint(120,180,BLACK);
		}
    RTC_ClearITPendingBit(RTC_IT_SEC);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    
  }
}
